package com.sky.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Clazz;
import com.sky.domain.Student;
import com.sky.service.ClazzService;
import com.sky.service.StudentService;
import com.sky.util.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 尹稳健~
 * @description: 控制器-管理学生信息页面
 * @version 1.0
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    // 注入业务对象
    @Autowired
    @Qualifier("studentServiceImpl")
    private StudentService studentService;

    // 注入业务对象
    @Autowired
    @Qualifier("clazzServiceImpl")
    private ClazzService clazzService;

    //存储预返回页面的数据对象
    private Map<String,Object> result = new HashMap<String, Object>();

    /**
     * @description: 跳转到学生信息管理页面
     * @param: org.springframework.ui.Model;
     * @return: java.lang.String
     */
    @RequestMapping("/goStudentListPage")
    public String goStudentListPage(Model model){
        List<Clazz> clazzList = clazzService.selectAll();
        //向页面发送一个存储着Clazz的List对象
        model.addAttribute("clazzList",clazzList);
        return "/student/studentList";
    }

    /**
     * @description: 分页查询学生信息列表:根据学生与班级名查询指定/全部学生信息列表
     * @param: page
     * @param: rows
     * @param: studentname
     * @param: clazzname
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/getStudentList")
    @ResponseBody
    public Map<String,Object> getStudentList(Integer page,Integer rows,String studentName,String clazzName){
        Student student = new Student(studentName, clazzName);
        //设置每页的记录数
        PageHelper.startPage(page, rows);
        //根据班级与学生名获取指定或全部学生信息列表
        List<Student> list = studentService.getStudentList(student);
        //封装信息列表
        PageInfo<Student> pageInfo = new PageInfo<Student>(list);
        //获取总记录数
        long total = pageInfo.getTotal();
        //获取当前页数据列表
        List<Student> studentList = pageInfo.getList();
        //存储数据对象
        result.put("total", total);
        result.put("rows", studentList);
        return result;
    }

    /**
     * @description: 添加学生信息
     * @param: student
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/addStudent")
    @ResponseBody
    public Map<String, Object> getStudentList(Student student){
        System.out.println(student);
        //判断学号是否已存在
        if(studentService.findBySno(student) != null){
            result.put("success",false);
            result.put("msg","该学号已经存在! 请修改后重试!");
            return result;
        }
        // 添加操作
        studentService.addStudent(student);
        result.put("success",true);
        System.out.println(result);
        return result;
    }

    /**
     * @description: 根据id修改指定的学生信息
     * @param: student
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/editStudent")
    @ResponseBody
    public Map<String, Object> editStudent(Student student){
        studentService.editStudent(student);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 根据id删除指定的学生信息
     * @param: ids
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/deleteStudent")
    @ResponseBody
    public Map<String, Object> deleteStudent(@RequestParam(value = "ids[]" ,required = true) Integer[] ids){
        studentService.deleteStudent(ids);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 上传头像-原理:将头像上传到项目发布目录中,通过读取数据库中的头像路径来获取头像
     * @param: photo
     * @param: request
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/uploadPhoto")
    @ResponseBody
    public Map<String ,Object > uploadPhoto(MultipartFile photo, HttpServletRequest request){
        //存储头像的本地目录
        final String dirPath = request.getSession().getServletContext().getRealPath("/upload/student_portrait/");
        //存储头像的项目发布目录
        final String portraitPath = request.getContextPath() + "/upload/student_portrait/";
        //返回头像的上传结果
        return UploadFile.getUploadResult(photo, dirPath, portraitPath);
    }
}
